<?php

/* * ************************************************************************************************
  |
  | AudioShare - Audio Sharing Script
  | http://www.audiosharescript.com
  | webmaster@audiosharescript.com
  |
  |**************************************************************************************************
  |
  | By using this software you agree that you have read and acknowledged our End-User License
  | Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
  |
  | Copyright (c) 2010 AudioShareScript.com. All rights reserved.
  |************************************************************************************************* */

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$mainmenu = "8";
STemplate::assign('mainmenu', $mainmenu);

// Verify valid audio
if (is_numeric($_REQUEST[audioid])) {
    $audioid = $_REQUEST[audioid];
    STemplate::assign('audioid', $audioid);
} else {
    $error = "$lang[840]";
}
if ($_SESSION[USERID] != "" && $_SESSION[USERID] >= 0 && is_numeric($_SESSION[USERID])) {

    $alreadyAdded = array();
    $playlistsAdded = array();

    ## Playlists to which audio is already added to...
    $query = "select * from playlist_audios where AUDIOID='" . $audioid . "' and USERID='" . $_SESSION[USERID] . "'";
    $executequery = $conn->execute($query);
    $alreadyAdded = $executequery->getarray();
    foreach ($alreadyAdded as $key => $val) {
        $playlistsAdded[] = $val[PLAYLISTID];
    }

    ## get the list of the user's playlists...
    $queryplaylist = "SELECT * FROM `playlists` WHERE USERID='" . $_SESSION[USERID] . "'";
    $executequeryplaylist = $conn->execute($queryplaylist);
    $playlistsarray = $executequeryplaylist->getarray();
    STemplate::assign('playlistsarray', $playlistsarray);

    $title = $_REQUEST[title];
    STemplate::assign('title', $title);

    $playlists_selected = array();


    if (isset($_POST["playlists"]) && !empty($_POST["playlists"])) {

        $playlists_selected = $_POST[playlists];
        $playlists_str = implode(",", $playlists_selected);

        $insert_str = "";
        $updatedPlaylists = array();
        foreach ($playlists_selected as $key => $val) {
            $playlistID = (int) $val;
            if (!in_array($playlistID, $playlistsAdded)) {
                $insert_str .= "(" . $playlistID . "," . $audioid . "," . $_SESSION[USERID] . ",now() ),";
                $playlistsAdded[] = $playlistID;
            }
            $updatedPlaylists[] = $playlistID;
        }

        ## remove last ',' from the insert string...
        $insert_str = substr($insert_str, 0, -1);

        ## add audios to the playlist...	
        $query = "Insert into playlist_audios values " . $insert_str;
        $executequery = $conn->execute($query);
        ## audios added to playlist...
        ## create xml files for the playlist...
        if (!empty($updatedPlaylists))
            create_playlist_xml($updatedPlaylists);
        ## xml files for the playlist created...	
    }
    STemplate::assign('playlistsAdded', $playlistsAdded);
}
STemplate::assign('user_id', $_SESSION[USERID]);


if ($error == "") {
    if (does_audio_exist($audioid) && private_audio_access_check($audioid, $_SESSION[USERID])) {
        $query = "SELECT * FROM audios WHERE AUDIOID='" . mysql_real_escape_string($audioid) . "'";
        $executequery = $conn->execute($query);
        $audioarray = $executequery->getarray();
        $audioDate=date("Y",strtotime($audioarray[0]["date_added"]));
        $catName=fetch_category_name_from_id($audioarray[0]["categories"]);
        
        STemplate::assign('catName',$catName);
        STemplate::assign('audioDate', $audioDate);
        STemplate::assign('audioarray', $audioarray);
        $audiouserid = $audioarray[0][USERID];

        STemplate::assign('pagetitle', stripslashes($audioarray[0][title]) . " $lang[841]");

        // Related Audios Begin
        $audiotags[] = $audioarray[0][tags];
        $audiotags[0] = str_replace("'", "''", $audiotags[0]);
        $audiotags[0] = str_replace("  ", "", $audiotags[0]);
        $audiotags[0] = str_replace("-", "", $audiotags[0]);
        $audiotagssplit = explode(" ", $audiotags[0]);
        $stripapos = str_replace("'", "''", $audioarray[0][tags]);
        $audiotagsstr = "%" . $stripapos . "%' ";

        if (count($audiotagssplit) > 1) {
            for ($i = 0; $i < count($audiotagssplit); $i++) {
                if ($audiotagssplit[$i] != "" && $audiotagssplit[$i] != "-" && $audiotagssplit[$i] != " ") {
                    $audiotagsstr.="or tags like '%$audiotagssplit[$i]%' ";
                    $audiotagsstr.="or title like '%$audiotagssplit[$i]%' ";
                }
            }
        }
        $audiotagsstr .= "' ";
        $maxrelvids = $config['max_related_audios_pv'];
        $query2 = "SELECT * FROM audios WHERE (tags like '$audiotagsstr') AND AUDIOID!='" . mysql_real_escape_string($audioid) . "' order by rand() limit $maxrelvids";
        $executequery2 = $conn->execute($query2);

        $relatedaudios = $executequery2->getarray();
        STemplate::assign('relatedaudios', $relatedaudios);
        // Related Audios End
        // Member Audios Begin
        $maxmemvids = $config['max_user_audios_pv'];
        $query3 = "SELECT * FROM audios WHERE USERID='$audiouserid' AND AUDIOID!='" . mysql_real_escape_string($audioid) . "' order by AUDIOID desc limit $maxmemvids";
        $executequery3 = $conn->execute($query3);
        $memberaudios = $executequery3->getarray();
        STemplate::assign('memberaudios', $memberaudios);
        // Member Audios End

        $USERID = $audioarray[0][USERID];
        STemplate::assign('USERID', $USERID);
        $AUDIOID = $audioarray[0][AUDIOID];
        STemplate::assign('AUDIOID', $AUDIOID);

        update_viewcount_audio($AUDIOID);
        update_last_viewed_audio($AUDIOID);
        update_your_audios_viewed($USERID);
        insert_audios_viewed($AUDIOID);
        if (session_verification()) {
            update_audios_you_viewed($_SESSION[USERID]);
        }

        STemplate::assign('max_comments', $max_comments);
        $query = "SELECT * FROM audios_comments WHERE AUDIOID='$AUDIOID' ORDER BY CID desc";
        $executequery = $conn->execute($query);
        $showcomments = $executequery->getarray();
        STemplate::assign('showcomments', $showcomments);
        $showtheme = "playaudio.tpl";
    } else if (!private_audio_access_check($audioid, $_SESSION[USERID])) {
        $query = "SELECT USERID FROM audios WHERE AUDIOID='" . mysql_real_escape_string($audioid) . "'";
        $executequery = $conn->execute($query);
        $theowneruserid = $executequery->fields[USERID];
        $query = "SELECT username FROM members WHERE USERID='" . mysql_real_escape_string($theowneruserid) . "'";
        $executequery = $conn->execute($query);
        $theownerusername = $executequery->fields[username];

        $error = "$lang[842] <a href='" . $thebaseurl . "/" . $theownerusername . "'>" . $theownerusername . "</a>.";
    } else {
        $error = "$lang[843]";
    }
}

//TEMPLATES BEGIN
STemplate::assign('error', $error);
STemplate::assign('message', $message);

STemplate::display('header.tpl');
if ($error == "") {
    STemplate::display($showtheme);
} else {
    STemplate::display('playaudioerror.tpl');
}

//var_dump(file_exists("audios.php")."--");
STemplate::display('footer.tpl');
//TEMPLATES END
?>